//
//  StatusCellOriginalView.m
//  王雷Sinoweibo
//
//  Created by wanglei on 16/11/20.
//  Copyright © 2016年 wanglei. All rights reserved.
//
#define MAS_SHORTHAND
#define MAS_SHORTHAND_GLOBALS
#import "StatusCellOriginalView.h"
#import "UIlabel+extension.h"
#import "UIImageView+WebCache.h"
@class UIlabel_extension;
@implementation StatusCellOriginalView

-(void)setStatus:(Status *)status{
    [self.iconImageView sd_setImageWithURL:status.user.profile_url];
    self.nameLabel.text = status.user.name;
    self.mbrank_image.image = status.user.mbrank_image;
    self.textLabel.text = status.text;
    self.pictureView.imageURLs = status.imageURLs;
    //    根据数组中是否有数据动态调整约束对象,建立底部约束属性,这里涉及到约束对象和cell的复用问题
    //    解决约束对象的步奏,1.使用约束属性记录老的约束 2.删除老的约束对象 3.建立新的约束对象并储存到属性里面
//    详情参考http://www.jianshu.com/p/2dc1decb5947,需要强调的是这里只是想要修稿底部的约束,而不是要对其他所有方向的约束进行重置,所以mas_remakeConstraints方法在这里不太合适
    
    //     2.删除老的约束对象
//    产考http://www.jianshu.com/p/4546f0fde96b了解约束添加和清除的本质,加深对uninstall的了解
    [self.bottomConstraint uninstall];
    if((self.pictureView.imageURLs != nil)&&(self.pictureView.imageURLs.count != 0)){
        //        和最初设置的约束是相同的,这里只要更新一下就好了
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            //    3.添加新的约束
            self.bottomConstraint = make.bottom.equalTo(_pictureView.mas_bottom).offset(StatusCellMargin);
        }];
    }else{
        //        如果返回的数组是空的,那么重新进行约束
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraint = make.bottom.equalTo(_textLabel.mas_bottom).offset(StatusCellMargin);
        }];
    }
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    self.backgroundColor = [UIColor whiteColor];
    [self setUi];
    return self;
}

- (void)setUi{
    //    此步骤非常关键,正是这样才能确保加入到父视图的imageview和懒加载获取的imageview是同一个;下面注释的写法是有问题的,就是
    //    self.iconImageView 和 _iconImageView 不是一个控件,约束不会执行
    /* [self addSubview:self.iconImageView];
     [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.left.mas_equalTo(self.mas_left).offset(StatusCellMargin);
     make.top.mas_equalTo(self.mas_top).offset(StatusCellMargin);
     NSValue *sizeValue = [NSValue valueWithCGSize:CGSizeMake(40, 40)];
     make.size.equalTo(sizeValue);
     }];
     */
    UIImageView *iconImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_default_big"]];
    UILabel *nameLabel = [[UIlabel_extension alloc]initWithtext:@"同学们好" fontsize:15 textColor:themColor Alignment:NSTextAlignmentCenter];
    UIImageView *mbrank_image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"common_icon_membership"]];
    UILabel *timeLabel = [[UIlabel_extension alloc]initWithtext:@"22:22" fontsize:12 textColor:[UIColor lightGrayColor] Alignment:NSTextAlignmentCenter];
    UILabel *sourceLabel = [[UIlabel_extension alloc]initWithtext:@"隔壁老王" fontsize:12 textColor:[UIColor lightGrayColor] Alignment:NSTextAlignmentCenter];
    UIImageView *verified_type_image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"avatar_vip"]];
    UILabel *textLabel = [[UIlabel_extension alloc]initWithtext:@"听说下雨天巧克力和辣条更配哦" fontsize:14 textColor:[UIColor darkGrayColor] Alignment:NSTextAlignmentLeft];
    UIView *view = [[UIView alloc]init];
    view.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1];
    
    //    注意这里调用init构造方法,也会造成调用StatusPictureView的initWithFrame方法
    //    StatusPictureView *pictureView = [[StatusPictureView alloc]init];
    StatusPictureView *pictureView = [[StatusPictureView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    
    self.iconImageView = iconImageView;
    self.nameLabel = nameLabel;
    self.mbrank_image = mbrank_image;
    self.timeLabel = timeLabel;
    self.sourceLabel = sourceLabel;
    self.verified_type_image = verified_type_image;
    self.textLabel = textLabel;
    self.pictureView = pictureView;
    self.sepView = view;
    
    [self addSubview:_iconImageView];
    [self addSubview:_nameLabel];
    [self addSubview:_mbrank_image];
    [self addSubview:_timeLabel];
    [self addSubview:_sourceLabel];
    [self addSubview:_verified_type_image];
    [self addSubview:_textLabel];
    [self addSubview:_pictureView];
    [self addSubview:_sepView];
    
    [_iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left).offset(StatusCellMargin);
        make.top.mas_equalTo(_sepView.mas_top).offset(StatusCellMargin);
        NSValue *sizeValue = [NSValue valueWithCGSize:CGSizeMake(40, 40)];
        make.size.equalTo(sizeValue);
    }];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImageView.mas_right).offset(StatusCellMargin);
        make.top.equalTo(_iconImageView.mas_top);
    }];
    
    [_mbrank_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLabel.mas_right).offset(StatusCellMargin);
        make.top.equalTo(_iconImageView.mas_top);
    }];
    
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_iconImageView.mas_bottom);
        make.left.equalTo(_iconImageView.mas_right).offset(StatusCellMargin);
        
    }];
    
    [_sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_timeLabel.mas_right).offset(StatusCellMargin);
        make.top.equalTo(_timeLabel.mas_top);
    }];
    
    [_verified_type_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(_iconImageView.mas_bottom);
        make.right.equalTo(_iconImageView.mas_right);
        
    }];
    [_textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconImageView.mas_left);
        make.top.equalTo(_iconImageView.mas_bottom).offset(StatusCellMargin);
        make.right.equalTo(self.mas_right).offset(-StatusCellMargin);
    }];
    
    [_pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_textLabel.mas_left);
        make.top.equalTo(_textLabel.mas_bottom);
        make.size.equalTo(CGSizeMake(100, 100));
    }];
    
    [_sepView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.equalTo(10);
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        //        1.使用约束属性记录老的约束
        self.bottomConstraint =  make.bottom.equalTo(_pictureView.mas_bottom).offset(StatusCellMargin);
        //        注意:StatusCell里面有本视图StatusCellOriginalView的其他方向的约束

    }];
    //将用户头像处理成圆角
    _iconImageView.layer.cornerRadius = 20;
    _iconImageView.layer.masksToBounds = true;
}
@end
